import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../utils/utils.dart';

extension NumExtension on num {
  SizedBox get vGap => SizedBox(width: w);

  SizedBox get hGap => SizedBox(height: h);

  BorderRadiusGeometry get radius => BorderRadius.circular(r);
}

extension DoubleExtension on double {
  String getIntegerString() {
    return toString().split('.')[0];
  }

  String getDecimalString() {
    var decimal = toString().split('.')[1];
    switch(decimal.length) {
      case 0: return '00';
      case 1: return '${decimal}0';
      case 2: return decimal;
      default: return decimal.substring(0, 2);
    }
  }
}

extension StringExtension on String {
  String removeZero() {
    var result = this;
    while (result.contains(".") && result.endsWith("0")) {
      result = result.substringBeforeLast("0");
    }
    if (result.endsWith(".")) {
      result = result.substringBeforeLast(".");
    }
    return result;
  }

  String substringBeforeLast(String delimiter) {
    var index = lastIndexOf(delimiter);
    return index == -1 ? this : substring(0, index);
  }

  String substringAfter(String delimiter) {
    var index = indexOf(delimiter);
    return index == -1 ? this : substring(index + delimiter.length, length);
  }
}

extension CopyExtension on String {
  void copy() async {
    await Clipboard.setData(ClipboardData(text: this));

    toast('Copy Completed'.tr);
  }
}
